﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using DDU.DataModel;
using System.Windows;
using DDU.Library.Extensions;
using System.Net;
using System.Xml.Linq;
using System.Globalization;

namespace DDU.ViewModel
{
    public class HomeViewModel : MainViewModel
    {
        WebClient client = new WebClient();

        public HomeViewModel()
        {
            client.DownloadStringCompleted -= client_DownloadStringCompleted;
            client.DownloadStringCompleted += client_DownloadStringCompleted;

            client.DownloadStringAsync(new Uri(ConstKeys.RecommendBooksUrl, UriKind.RelativeOrAbsolute));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            NumberFormatInfo formatProvider = new CultureInfo("en-US").NumberFormat;

            if (e.Error == null)
            {
                RecordData(e.Result.Length);

                XDocument document = XDocument.Parse(e.Result);
                IEnumerable<RecommendBooksInfo> books = (from element in document.Descendants("item")
                                                         select new RecommendBooksInfo()
                                                         {
                                                             BookName = element.Element("name").Value,
                                                             Author = element.Element("author").Value,
                                                             Barcode = element.Element("barcode").Value,
                                                             Image = element.Element("image").Value,
                                                             ISBN = element.Element("isbn").Value,
                                                             Press = element.Element("press").Value,
                                                             Url = HttpUtility.UrlDecode(element.Element("url").Value),
                                                             Preferential = Convert.ToDouble(element.Element("preferential").Value, formatProvider),
                                                             Price = Convert.ToDouble(element.Element("price").Value, formatProvider)
                                                         }).Take(4);
                RecommendBooks = new ObservableCollection<RecommendBooksInfo>(books);

            }
        }

        public bool InVacation
        {
            get
            {
                if (CurrentSemester == null)
                {
                    return true;
                }
                return Database.CollectionOfVacations.Count(v => v.StartTime < DateTime.Today && v.EndTime > DateTime.Today) > 0;
            }
        }

        public bool SemesterAvailable
        {
            get
            {
                return !InVacation;
            }
        }

        public int TodaySchedulesCount
        {
            get
            {
                //return 0;
                return TodaySchedules == null ? 0 : TodaySchedules.Count();
            }
        }

        public string ExamNotificationString
        {
            get
            {
                if (TodayExamCount > 0)
                {
                    return string.Format(Application.Current.FindResource<string>("IDS_HOME_EXAM_NOTIFICATION_STRING"), TodayExamCount);
                }
                return string.Empty;
            }
        }

        public int TodayExamCount
        {
            get
            {
                if (IsDesignMode)
                {
                    return 2;
                }
                DateTime now = DateTime.Today;
                return Database.CollectionOfExaminationCourses.Count(e => e.StartTime > now && e.StartTime < now.AddDays(1));
            }
        }

        public ObservableCollection<ScheduleInfo> TodaySchedules
        {
            get
            {
                if (CurrentSemester == null)
                {
                    return null;
                }
                return new ObservableCollection<ScheduleInfo>(Database.CollectionOfSchedules.Where(s => s.SemesterGuid == CurrentSemester.Guid && s.DayOfWeek == DateTime.Now.DayOfWeek));
            }
        }

        private ObservableCollection<RecommendBooksInfo> recommendBooks;
        public ObservableCollection<RecommendBooksInfo> RecommendBooks
        {
            get { return recommendBooks; }
            set
            {
                recommendBooks = value;
                RaisePropertyChanged(() => RecommendBooks);
            }
        }

    }
}
